<?php

class LoginModel extends dmq\Model
{
	public function login( $auth, $username, $password )
	{
		$password = sha1( $password );
		
		$stmt = $this->db->prepare( 'SELECT userId, name, email FROM Users WHERE name = ? && password = UNHEX( ? )' );
		$stmt->bind_param( 'ss', $username, $password );
		$stmt->execute();
		$stmt->bind_result( $row_id, $row_name, $row_email );
		$stmt->fetch();
		if ( $row_id )
		{
			require_once 'php/obj/User.php';
			$auth->login( new User( $row_id, $row_name, null, $row_email ) );
		}
		$stmt->close();

		if ( !$auth->isLoggedIn() )
		{
			throw new Exception( 'Invalid username or password.' );
		}
	}
	
	public function logout( $auth )
	{
		$auth->logout();
	}
}
